package com.atguigu.champter2;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;


public class $1_flink_Bander_buck {
    public static void main(String[] args) throws Exception {
        //创建执行环境
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

        DataSource<String> ds = env.readTextFile("datas/WordCount.txt");

        ds
                .flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String words, Collector<String> collector) throws Exception {
                String[] word = words.split(" ");
                for (String s : word) {
                    collector.collect(s);
                }
            }
        })
                .map(new MapFunction<String, Tuple2<String,Integer>>() {
                    @Override
                    public Tuple2<String, Integer> map(String value) throws Exception {
                        org.apache.flink.api.java.tuple.Tuple2<String, Integer> tuple2 = new org.apache.flink.api.java.tuple.Tuple2<>(value, 1);
                        return tuple2;
                    }
                })
                .groupBy(0)
                .sum(1)
                .print();
    }
}
